package com.example.search.service.impl;

import com.example.search.dao.GoodsRepository;
import com.example.search.model.Goods;
import com.example.search.service.IGoodsService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;

import java.util.Optional;

/**
 * @Auther: nanhuanh
 * @Date: 2019/3/6 16:08
 * @Description:
 */
@Slf4j
@Service
public class GoodsServiceImpl implements IGoodsService {

    @Autowired
    @Qualifier("goodsRepository")
    private GoodsRepository goodsRepository;


    @Override
    public Optional<Goods> findById(String id) {
        Optional<Goods> optionalShop = null;
        try {
            optionalShop = goodsRepository.findById(id);
        } catch (Exception e) {
            log.error(">>>>>>>>>>>>>>>>>>>>>>", e);
        }
        return optionalShop;
    }

    @Override
    public Goods save(Goods blog) {
        Goods shop = null;
        try {
            shop = goodsRepository.save(blog);
        } catch (Exception e) {
            log.error(">>>>>>>>>>>>>>>>>>>>>>", e);
        }
        return shop;
    }

    @Override
    public void delete(Goods blog) {
        try {
            goodsRepository.delete(blog);
        } catch (Exception e) {
            log.error(">>>>>>>>>>>>>>>>>>>>>>", e);
        }
    }

    @Override
    public Goods update(Goods blog) {
        Goods tmp = null;
        try {
            if (StringUtils.isBlank(blog.getId())) {
                return null;
            }
            Optional<Goods> optional = findById(blog.getId());
            if (optional != null) {
                tmp = optional.get();
            }
            if (tmp != null) {
                BeanUtils.copyProperties(blog, tmp);
                goodsRepository.save(tmp);
            }
        } catch (Exception e) {
            log.error(">>>>>>>>>>>>>>>>>>>>>>", e);
        }
        return tmp;
    }

    @Override
    public Optional<Goods> findOne(String id) {
        Optional<Goods> optionalShop = null;
        try {
            optionalShop = goodsRepository.findById(id);
        } catch (Exception e) {
            log.error(">>>>>>>>>>>>>>>>>>>>>>", e);
        }
        return optionalShop;
    }

    @Override
    public Iterable<Goods> findAll() {
        Iterable<Goods> shops = null;
        try {
            shops = goodsRepository.findAll();
        } catch (Exception e) {
            log.error(">>>>>>>>>>>>>>>>>>>>>>", e);
        }
        return shops;
    }

    @Override
    public Page<Goods> findByShopName(String shopName, Pageable pageable) {
        Page<Goods> shopPage = null;
        try {
            shopPage = goodsRepository.findByGoodsName(shopName, pageable);
        } catch (Exception e) {
            log.error(">>>>>>>>>>>>>>>>>>>>>>", e);
        }
        return shopPage;
    }

    @Override
    public Page<Goods> findByShopCode(String shopCode, Pageable pageable) {
        Page<Goods> shopPage = null;
        try {
            shopPage = goodsRepository.findByShopCode(shopCode, pageable);
        } catch (Exception e) {
            log.error(">>>>>>>>>>>>>>>>>>>>>>", e);
        }
        return shopPage;
    }

}
